
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'MD3.OpenTransactionsCheck') AND type in (N'P', N'PC'))
	DROP PROCEDURE MD3.OpenTransactionsCheck
GO

CREATE PROC MD3.OpenTransactionsCheck
AS
BEGIN
	SET NOCOUNT ON
	
	--Throws an error if any open transactions are detected. This means we have a bug in a script somewhere.  
	IF @@TRANCOUNT = 0
	BEGIN
		RETURN
	END 
		
	IF (@@TRANCOUNT > 0)
	BEGIN

		ROLLBACK TRANSACTION;

		DECLARE @Msg VARCHAR(200)
		SELECT @Msg = 'An open transaction was detected.  We rolled it back and are failing MD3.'
		
		EXEC MD3.InsertLogMessage
			@Message = @Msg,
			@Severity = 'E',
			@ProcedureName = 'MD3.OpenTransactionsCheck',
			@ScriptName = NULL;

		RAISERROR (@Msg, 16,1);
		RETURN 1;
	
	END

END 
GO
